//
//  AppDelegate.swift
//  sinaweibo
//
//  Created by icefs2 on 15/11/8.
//  Copyright © 2015年 icefs2. All rights reserved.
//

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//        let accout = UserAccout.loadAccout()
//        print(accout)
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        //注册通知
        registNotifivation()
        window?.rootViewController = defaultviewcontroll()
        //外观设置,全局有效
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        return true
    }
    //MARK:监听通知
    private func registNotifivation() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchToRootVC:", name: AppSwitchRootViewControler, object: nil)
    }
    @objc private func switchToRootVC(n: NSNotification) {
        if(n.object as? String) != nil {
            window?.rootViewController = WelcomeViewController()
            return
        }
        window?.rootViewController = MainViewController()
    }
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
     //MARK: 判断用户是否登陆,并且是否更新版本
    private func defaultviewcontroll() -> UIViewController {
       return IsNewVersion() ? NewFeatureController() : WelcomeViewController()
        }
    
    
     //MARK: 检测是否是新版本
    private func IsNewVersion() -> Bool {
        //新
        let varsion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let varsionstr = Double(varsion)
        //旧
        let key = "sandBoxKey"
        let userdefaults = NSUserDefaults.standardUserDefaults()
        let lastVersion = userdefaults.doubleForKey(key)
        print(lastVersion)
        
        //3.保存新斑斑
        userdefaults.setDouble(varsionstr!, forKey: key)
        //同步沙盒
        userdefaults.synchronize()
        
        return varsionstr! > lastVersion
    }
    

}

